<?php

namespace app\admin\controller;


use app\admin\model\ArticleCategory;

class ArticleCategoryController extends BaseController
{
    public function index()
    {

        return $this->fetch();
    }
    public function read()
    {
        $category = new ArticleCategory();
        $data  = $category->select();

        if (isset($_GET['data_type'])&& !empty($_GET['data_type'])){
            $alias = ['category_title' => 'name', 'category_parent' => 'pid', 'category_id' => 'id'];
            $tree_list = tree($data, 'category_id', $alias);
            array_unshift($tree_list, ['name' => '顶级分类', 'pid' => 0, 'id' => 0]);
            return json($tree_list);
        }
        return json(array('code' => 0, 'msg' => "", 'data' => $data, 'count' => 100));
    }
    public function del()
    {
        $category = (new ArticleCategory())->where(input('post.'))->delete();
        if ($category) {
            return json(['code' => 0, 'msg' => '删除成功']);
        } else {
            return json(['code' => 500, 'msg' => '删除失败']);
        }
    }
    public function add()
    {
        $category= (new ArticleCategory());

        $category->save(input('post.'));
        if ($category) {
            return json(['code' => 0, 'msg' => '添加成功']);
        } else {
            return json(['code' => 500, 'msg' => '添加失败']);
        }
    }
    public function upd()
    {
        $category= (new ArticleCategory());
        $post_data = input('post.');
        $category->isUpdate(true)->save($post_data);
        if ($category) {
            return json(['code' => 0, 'msg' => '更新成功']);
        } else {
            return json(['code' => 500, 'msg' => '更新失败']);
        }
    }

}
